package sg.edu.nus.iss.vmcs.store;

/*
 * Copyright 2003 ISS.
 * The contents contained in this document may not be reproduced in any
 * form or by any means, without the written permission of ISS, other
 * than for the purpose for which it has been supplied.
 *
 */

/**
 * An abstract class that defines a storage class that store an array of items
 *
 * @version 3.0 5/07/2003
 * @author Olivo Miotto, Pang Ping Li
 */

public abstract class Store {
	public final static int CASH  = 1;
	public final static int PRODUCT = 2;

	protected int size;

	protected StoreItem items[];

	/**
	 * Default constructor that creates a Store
	 */
	public Store() {
	}

	/**
	 * Constructor that creates a Store with the number of maximum item it can
	 * store
	 * 
	 * @param itemn
	 *            the maximum size of the store
	 */
	public Store(int itemn) {
		size = itemn;
		items = new StoreItem[size];
	}

	/**
	 * Set the size of the Store
	 * 
	 * @param sz
	 *            the size of the store
	 */
	public void setStoreSize(int sz) {
		size = sz;
		items = new StoreItem[size];
	}

	/**
	 * Get the Array of the StoreItem
	 * 
	 * @return the array of the StoreItem
	 */
	public StoreItem[] getItems() {
		return items;
	}

	/**
	 * insert an StoreItem into the Store base on the index
	 * 
	 * @param idx
	 *            the index to insert the StoreItem
	 * @param object
	 *            the StoreItem to be inserted into the Store
	 */
	public void addItem(int idx, StoreItem object) {
		if ((idx >= size) || (idx < 0))
			return;
		items[idx] = object;
	}

	/**
	 * Get the StoreItem based on an index
	 * 
	 * @param idx
	 *            the index to retrieve the StoreItem
	 * @return the StoreItem based on the index
	 */
	public StoreItem getStoreItem(int idx) {
		if ((idx >= size) || (idx < 0))
            return null;
		return items[idx];
	}

	/**
	 * Get the StoreItem based on an name
	 * 
	 * @param name
	 *            the string to compare with the name of StoreItem in the Store
	 * @return the StoreItem based on the name
	 */
	public StoreObject findObject(String name) {
		String en;
		StoreObject so;
		int i;

		for (i = 0; i < size; i++) {
			if (items[i] == null)
				return null;
			so = items[i].getContent();
			if (so == null)
				return null;
			en = so.getName();
			if (en != null) {
				if (en.compareTo(name) == 0)
					return so;
			}
		}
		return null;
	}


	/**
	 * Set the quantity of a StoreItem based on the index
	 * 
	 * @param idx
	 *            the index of the StoreItem to be edited
	 * @param qty
	 *            the new quantity of the StoreItem
	 */
	public void setQuantity(int idx, int qty) {
		System.out.println("Store: setQauntity- qty=" + qty);
		if ((idx >= size) || (idx < 0))
			return;
		items[idx].setQuantity(qty);
	}

	/**
	 * Get the Store Size
	 * 
	 * @return the store size
	 */
	public int getStoreSize() {
		return size;
	}

}
